package com.example.itces.controller;

import com.example.itces.domain.JgQuestion;
import com.example.itces.domain.Question;
import com.example.itces.service.QuestionService;
import com.example.itces.utils.ResponData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class QuestionController {

    @Autowired
    QuestionService questionService ;

    //插入java单选题
    @RequestMapping("/page/teacher/addqt")
    @ResponseBody
    public ResponData addJavaQuestion(Question question, ResponData responData){

        int i = questionService.insertQuestion(question);
        if (i>0){
            return ResponData.build(0,"插入成功");
        }else {
            return ResponData.build(1,"插入失败");
        }
    }

    //插入web单选题
    @RequestMapping("/page/teacher/addqw")
    @ResponseBody
    public ResponData addWEBQuestion(Question question){

        int i = questionService.insertQuestion(question);
        if (i>0){
            return ResponData.build(0,"插入成功");
        }else {
            return ResponData.build(1,"插入失败");
        }
    }

    //插入JAVA判断题
    @RequestMapping("/page/teacher/addjgqt")
    @ResponseBody
    public ResponData addJAVAJgQuestion(JgQuestion jgQuestion){
        int i = questionService.insertJgQuestion(jgQuestion);
        if (i>0){
            return ResponData.build(0,"插入成功");

        }else {
            return ResponData.build(1,"插入失败");
        }
    }

    //插入WEB判断题
    @RequestMapping("/page/teacher/addjgqw")
    @ResponseBody
    public ResponData addWEBJgQuestion(JgQuestion jgQuestion){
        int i = questionService.insertJgQuestion(jgQuestion);
        if (i>0){
            return ResponData.build(0,"插入成功");

        }else {
            return ResponData.build(1,"插入失败");
        }
    }


    @RequestMapping("/page/user/getQuestion")
    @ResponseBody
    public ResponData getQuestion(@RequestParam("project") String project , HttpSession session){
//        int size=10;
        List<Question> list=questionService.getJavaQuestion(project);
        List<JgQuestion> jqlist=questionService.getJavaJgQuestion(project);
        Map<String,String> ansewrMap=new HashMap<>();
        for(JgQuestion jq :jqlist){
            ansewrMap.put("j_"+jq.getId(),jq.getRightAnswer());
            jq.setRightAnswer("");
        }
        for(Question question :list){
            ansewrMap.put(question.getId()+"",question.getRightAnswer());
            question.setRightAnswer("");
        }
        System.out.println(ansewrMap);
        Map<String,Object> map=new HashMap<>();
        map.put("sqb",list);
        map.put("jqb",jqlist);
        System.out.println(map);
        session.setAttribute("answer",ansewrMap);
        return ResponData.build(1,"success",map);
    }

    @RequestMapping(value = "/page/user/putAnswer",method = RequestMethod.POST)
    @ResponseBody
    public ResponData putAnswer(@RequestParam Map<String,String> map,HttpSession session){
        System.out.println(map);
        Map<String,String> answermap= (Map<String, String>) session.getAttribute("answer");
        System.out.println(answermap);
        int score=0;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if(answermap.get(entry.getKey()).equals(entry.getValue())) {
                if(entry.getKey().split("_")[0].equals("j")){
                    score += 2;
                }else {
                    score += 4;
                }
            }
        }
        return ResponData.build(1,score+"",answermap);
    }



}
